<template>
	<view>
		<view style="display: flex;justify-content: center;align-items: center;margin-top: 100rpx;">
			<view
				style="border-radius: 50%;border: 1px solid #ccc;width: 375rpx;height: 375rpx;display: flex;justify-content: center;align-items: center;overflow: hidden;">
				<image v-if="!base64" @click="photoGraph" src="../../static/photoStart.pic.jpg"
					style="width: 150rpx;height: 150rpx;">
				</image>
				<image v-else @click="photoGraph" :src="base64" style="width: 375rpx;height: 375rpx" mode="aspectFit">
				</image>
			</view>

		</view>
		<view style="display: flex;justify-content: center;margin-top: 50rpx;color: #999;margin-bottom: 10px;">
			<text>
				请先拍摄您的正脸照片
			</text>
		</view>
		<view style="padding: 0 10px;">
			<uni-forms>
				<uni-forms-item label="姓名" required label-align="right" :label-width="80">
					<uni-easyinput v-model="name" placeholder="请输入姓名" />
				</uni-forms-item>
				<uni-forms-item label="身份证号" required label-align="right" :label-width="80">
					<uni-easyinput v-model="idCard" placeholder="请输入身份证号" />
				</uni-forms-item>
			</uni-forms>

		</view>

		<!-- <view style="display: flex;flex-direction: column;align-items: center;margin: 100rpx 0;color: #888;">
			姓名：
			<uni-easyinput placeholder="请输入姓名" v-model="name" style="margin-bottom: 10px;"></uni-easyinput>
			<uni-easyinput placeholder="请输入身份证号" v-model="idCard"></uni-easyinput>
			
		</view> -->
		<button :disabled="disable" type="primary" style="margin: 0 15px;" @click="callRequest">提交认证</button>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				base64: '',
				name: '',
				idCard: '',
				disable: false,
				userInfo: {},
			}
		},
		onShow() {

		},
		onLoad() {
			this.base64 = "";
			this.userInfo = uni.getStorageSync('userInfo');
			this.name = this.userInfo.UserName
			this.idCard = this.userInfo.IdNumber
		},
		onBackPress(options) {
			uni.reLaunch({
				url: '/pages/index/index?Id=' + uni.getStorageSync('Id')
			});
			return true
		},
		methods: {
			tempToBase64(tempFiles) {
				return new Promise((resolve, reject) => {
					///专门用来读文件的工具类
					const reader = new FileReader();
					reader.readAsDataURL(tempFiles);
					reader.onload = () => resolve(reader.result);
					reader.onerror = (error) => reject(error);
				});
			},
			photoGraph() {
				let this_ = this;
				uni.chooseImage({
					count: 6, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['camera'],
					success: async (res) => {
						let tempFiles = res.tempFiles[0]
						this.tempToBase64(tempFiles).then(val => {
							console.log(val);
							this.base64 = val
						})

						// uni.getFileSystemManager().readFile({
						// 	filePath: tempFiles.path,
						// 	encoding: 'base64',
						// 	success: (res) => {
						// 		// 避免获取不到文件类型
						// 		if (!tempFiles.type) {
						// 			uni.getImageInfo({
						// 				src: tempFiles.path,
						// 				success: (image) => {
						// 					// 拼接成完整的base64
						// 					this_.base64 =
						// 						`data:image/${image.type};base64,${res.data}`
						// 				},
						// 				fail: (err) => {
						// 					reject(err)
						// 				}
						// 			});
						// 		}
						// 		// 拼接成完整的base64
						// 		else this.base64 = `data:${tempFiles.type};base64,${res.data}`
						// 	},
						// 	fail: (err) => {
						// 		reject(err)
						// 	}
						// })
					}
				});
			},
			// 保存认证记录
			certification() {
				uni.request({
					url: this.$veriUrl + '/auth/VeriRecord',
					method: 'POST',
					data: {
						"phone": this.userInfo.UserPhone,
						"name": this.name,
						"idNumber": this.idCard,
						"state": true,
						VeriType: this.type,
						// remark: JSON.stringify(this.remark)
					}
				}).then(result => {
					uni.hideLoading()
					let [err, res] = result;
					if (err !== null) flag = true
					if (res.data.code == 200) {
						setTimeout(() => {
							uni.showToast({
								title: '验证成功',
								icon: 'none'
							})
							setTimeout(() => {
								uni.redirectTo({
									url: `/pages/signFor/signFor?Id=${uni.getStorageSync('Id')}&isType=true`
								})
							}, 500)
						}, 500)
					} else {
						uni.showToast({
							title: '验证失败,请尝试重新提交',
							icon: 'none'
						})
					}
				})
			},
			// 提交信息
			callRequest() {
				if (!this.base64) {
					uni.showToast({
						title: '请先拍照',
						icon: "none"
					});
					return;
				}
				this.disable = true;
				uni.showLoading({
					title: '加载中',
					mask: true,
				});
				this.$http('post', '/WeChat/wechatinformation/FaceRecognition', {
					number: this.idCard,
					base64Str: this.base64,
					name: this.name
				}).then(res => {
					this.disable = false;
					let resJSON = JSON.parse(res.Data);
					uni.hideLoading()
					setTimeout(() => {
						switch (resJSON.result.res) {
							case -1:
								uni.showToast({
									title: '身份证和姓名不一致',
									icon: "none"
								});
								break;
							case -2:
								uni.showToast({
									title: '公安库中无此身份证记录',
									icon: "none"
								});
								break;
							case -3:
								uni.showToast({
									title: '公安身份证库中没有此号码的照片',
									icon: "none"
								});
								break;
							case -4:
								uni.showToast({
									title: '照片参数不合格',
									icon: "none"
								});
								break;
							case -5:
								uni.showToast({
									title: '照片相片体积过大',
									icon: "none"
								});
								break;
							case -6:
								uni.showToast({
									title: '请检查图片编码',
									icon: "none"
								});
								break;
							case -7:
								uni.showToast({
									title: '照片相片体积过小',
									icon: "none"
								});
								break;
							case 1:
								uni.showLoading({
									title: '验证成功保存中',
									mask: true,
								});
								this.certification();
								// uni.showToast({
								//     title: '系统分析为同一人',
								//     icon: "none"
								// });

								break;
							case 2:
								uni.showToast({
									title: '系统分析可能为同一人',
									icon: "none"
								});
								break;
							case 3:
								uni.showToast({
									title: '系统分析为不是同一人',
									icon: "none"
								});
								break;
							case 4:
								uni.showToast({
									title: '没检测到人脸',
									icon: "none"
								});
								break;
							case 5:
								uni.showToast({
									title: '疑似非活体',
									icon: "none"
								});
								break;
							case 6:
								uni.showToast({
									title: '出现多张脸',
									icon: "none"
								});
								break;
							case 7:
								uni.showToast({
									title: '身份证和姓名一致，官方人脸比对失败',
									icon: "none"
								});
								break;
						}
					}, 100)


				})
			}
		}
	}
</script>